package com.yjx.service.impl;

import com.yjx.service.OperateTokenService;
import com.yjx.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class OperateTokenServiceImpl implements OperateTokenService {
    private static final Logger logger = LoggerFactory.getLogger(OperateTokenServiceImpl.class);
    @Value("${operateToken.timeout}")
    private String operateTimeout;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String getOperateToken() {
        logger.info("掉用Stater方法");
        //生成UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //放到redis中
        redisUtil.put(uuid,uuid,Long.valueOf(operateTimeout), TimeUnit.MINUTES);
        return uuid;
    }

    @Override
    public Boolean checkOperateToken(String operateToken) {
        String value = redisUtil.get(operateTimeout);
        if (value != null && (!value.equals(""))){
            //验证通过
            redisUtil.delete(operateTimeout);
            return true;
        }
        return false;
    }
}
